package com.unbook.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unbook.entity.School;
import com.unbook.mapper.ISchoolMapper;
import com.unbook.service.ISchoolService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class SchoolServiceImpl extends ServiceImpl<ISchoolMapper, School> implements ISchoolService {
    @Autowired
    ISchoolMapper schoolMapper;

    @Override
    public List<School> queryList(String name, String province, String city) {
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(School::getName, name);
        if (province != null && !province.isEmpty())
            queryWrapper.like(School::getProvince, province);
        if (city != null && !city.isEmpty())
            queryWrapper.like(School::getCity, city);
        return schoolMapper.selectList(queryWrapper);
    }

    @Override
    public List<School> importSchool(MultipartFile file) {
        try {
            List<School> schools = parseExcel(file);
            // 清空全表
            remove(new QueryWrapper<>());
            // 批量插入
//            saveBatch(schools, 1000);
            return schools;
        } catch (IOException e) {
//            throw new RuntimeException(e);
            // 导入失败
            return null;
        }
    }

    private List<School> parseExcel(MultipartFile file) throws IOException {
        List<School> schools = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), new AnalysisEventListener<HashMap<Integer, String>>() {
            String province;

            @Override
            public void invoke(HashMap<Integer, String> o, AnalysisContext analysisContext) {
                if (o.size() == 7 && o.get(0) != null && o.get(1) == null) {
                    province = o.get(0).split("（")[0];
                }
                if (o.size() == 7 && o.get(1) != null && o.get(2) != null && o.get(4) != null && o.get(5) != null) {
                    String oType = o.get(5);
                    short type = oType.equals("本科") ? (short) 0 : oType.equals("专科") ? (short) 1 : oType.equals("成人本科") ? (short) 2 : -1;
                    School school = School.builder()
                        .name(o.get(1))
                        .sId(o.get(2))
                        .province(province)
                        .city(o.get(4))
                        .type(type)
                        .remark(o.get(6))
                        .build();
                    schools.add(school);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 解析完成后的操作，可以在这里进行额外的处理
            }
        }).sheet().headRowNumber(3).doRead();

        return schools;
    }
}
